var rootPath = getRootPath();
var oriPhone;

$(function() {
	//滚动条置顶
	 $("body").prepend('<input type="text" id="focus" size="1">');
     $("#focus").focus();
    $("#focus").hide();
	var rleId = getUrlParam("rleId");
	$("#rleId").val(rleId);
	
	$.ajax({
		url : rootPath + '/verify/uid/rleId/' + rleId + '/dr/1',
		type : 'GET',
		success : function(data) {
			if ('0000' == data.returnCode) {
				var verify = data.returnObject;
				$("#company").val(verify.company);
				$("#address").val(verify.address);
				$("#intro").val(verify.intro);
				$("#contact").val(verify.contact);
				/*$("#job").val(verify.job);*/
				$("#phone").val(verify.phone);
				oriPhone = verify.phone;
				$("#wechat").val(verify.wechat);
				
				//选中已选的机构类别
				if(verify.category.categoryId != null && verify.category.categoryId != "") {
					$("#categoryId option[value='" + verify.category.categoryId + "']").prop("selected", "selected");
				}
				
				if(verify.province!=null && verify.province!=""){
					$("#province option[value='"+verify.province+"']").prop("selected","selected");
					$("#province").change();
				}
				if(verify.city!=null && verify.city!=""){
					$("#city option[value='"+verify.city+"']").prop("selected","selected");
					$("#city").change();
				}
				if(verify.area!=null && verify.area!=""){
					$("#area option[value='"+verify.area+"']").prop("selected","selected");
				}
			}
		},
		error : function(jqXHR, textStatus, errorThrown) {
			layer.alert("error");
		}
	});
	
	$("input[type='file']").each(function() {
		$(this).change(function() {
			$(this).siblings(".uploadFile").css("background","#EA9792");
			if($(this).val() == '') {
				$(this).siblings(".uploadFile").html("请上传");
			} else {			
				$(this).siblings(".uploadFile").html($(this)[0].files[0].name);
				$(this).siblings(".uploadFile").css("background","#B4B4B4");
				$(this).siblings(".msg").css("display","none");
			}
		});
	});
	
	$("#intro").bind("change,keydown,up", function() {
		$(this).val($(this).val().substring(0,800));
	});
	
	//上一步
	$("#prev").click(function() {
		var creType = $("#sort").val();
		parent.$("#content").prop("src", "realNameAuth.html?rleId=" + rleId + "&creType=" + creType);
	});
	
	$("#applyAuth").click(function() {
		
		if(!checkData()){
			return;
		}
		
		if ($("#company").val().length > 50) {
			$("#company").focus();
			showErrMsg("company","机构名称输入过长,请精简");
			return;
		}
		
		if ($("#address").val().length > 50) {
			$("#address").focus();
			showErrMsg("address","机构地址输入过长,请精简");
			return;
		}
		
		if ($("#intro").val().length > 800) {
			$("#intro").focus();
			showErrMsg("intro","简介字数过多,请精简");
			return;
		}
		
		if ($("#contact").val().length > 50) {
			$("#contact").focus();
			showErrMsg("contact","联系人姓名输入过长,请精简");
			return;
		}
		
	/*	if ($("#job").val().length > 50) {
			$("#job").focus();
			showErrMsg("job","联系人职位输入过长,请精简");
			return;
		}*/
		
		if($("#phone").val().length > 11) {
			$("#phone").focus();
			showErrMsg("phone", "手机号码输入过长");
			return;
		}
		
		if ($("#wechat").val().length > 50) {
			$("#wechat").focus();
			showErrMsg("wechat","微信账号输入过长");
			return;
		}
		
		showLoading();
		$("#specialFundAuthForm").ajaxSubmit({
			url : rootPath + '/verify/saveOrUpdateVerify',
			type: 'POST',
			contentType : 'application/json;charset=UTF-8',
			success : function(data) {
				hideLoading();
				if(data.returnCode == '0000'){
					layer.msg("操作成功",{icon:1,time:1000},function() {
						parent.$("#content").prop("src","applyResult.html?rleId=" + rleId + "&title=专项基金认证");
					});
				}
			},
			error: function (jqXHR, textStatus, errorThrown) {
				hideLoading();
				layer.alert("error");
			}
		});
		return false;
	});
});

function checkData(){
	if($.trim($("#company").val()) == '') {
		$("#company").focus();
		showErrMsg("company","请输入机构名称");
		return false;
	}
	if($("#province").val() == '' || $("#province").val() == 0) {
		$("#province").focus();
		showErrMsg("area","请选择省份");
		return false;
	}
	//台湾,香港,澳门等地暂无数据
	if($("#city option").length > 1 && $("#city").val() == '') {
		$("#city").focus();
		showErrMsg("area","请选择城市");
		return false;
	}
	//全国有5个不设区的地级市
	if($("#area option").length > 1 && $("#area").val() == '') {
		$("#area").focus();
		showErrMsg("area","请选择区");
		return false;
	}
	if($.trim($("#stuffFile").val()) == '') {
		$("#stuffFile").focus();
		showErrMsg("stuffFile","请上传资质认证证书");
		return false;
	}
	if($.trim($("#createConCopyFile").val()) == '') {
		$("#createConCopyFile").focus();
		showErrMsg("createConCopyFile","请上传设立合同扫描件");
		return false;
	}
	if($.trim($("#excuteConCopyFile").val()) == '') {
		$("#excuteConCopyFile").focus();
		showErrMsg("excuteConCopyFile","请上传执行合同扫描件");
		return false;
	}
	if($.trim($("#intro").val()) == '') {
		$("#intro").focus();
		showErrMsg("intro","请输入简介");
		return false;
	}
	if($.trim($("#contact").val()) == '') {
		$("#contact").focus();
		showErrMsg("contact","请输入联系人姓名");
		return false;
	}
/*	if($.trim($("#job").val()) == '') {
		$("#job").focus();
		showErrMsg("job","请输入联系人职位");
		return false;
	}*/
	if($.trim($("#phone").val()) == '') {
		$("#phone").focus();
		showErrMsg("phone","请输入手机号码");
		return false;
	} else {
		if(oriPhone != $("#phone").val() && !checkPhone($("#phone").val())) {
			$("#phone").focus();
			showErrMsg("phone","手机号码格式有误");
			return false;
		}
	}
	if($.trim($("#wechat").val()) == '') {
		$("#wechat").focus();
		showErrMsg("wechat","请输入微信账号");
		return false;
	}
	/*if($.trim($("#email").val()) == '') {
		$("#email").focus();
		showErrMsg("email","请输入邮箱地址");
		return false;
	} else {
		if(!checkEmail($("#email").val())) {
			$("#email").focus();
			showErrMsg("email","邮箱格式有误");
			return false;
		}
	}*/
	return true;
}